<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
<div class="fromPage">
    <form class="layui-form layui-form-pane" lay-filter="LAY-system-menus-form">
        <div class="layui-form-item">
            <label for="" class="layui-form-label">上级菜单</label>
            <div class="layui-input-block">
                <div id="parent"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label must">名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" lay-verify="required" lay-vertype="tips" class="layui-input" placeholder="如：system.index">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label must">显示名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="display_name" lay-verify="required" lay-vertype="tips" class="layui-input" placeholder="如：系统管理">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline"> <label class="layui-form-label">图标</label>
                <div class="layui-input-inline">
                    <input type="text" name="icon" class="layui-input" id="icon" placeholder="请选择图标">
                </div>
            </div>
            <div class="layui-inline"> <label class="layui-form-label">路由地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="url" class="layui-input" placeholder="请输入路由地址">
                </div>
            </div>
        </div>
        <div class="layui-form-item" pane="">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="radio" name="genre" lay-filter="genre" value="1" title="菜单" checked>
                <input type="radio" name="genre" lay-filter="genre" value="2" title="按钮" >
            </div>
        </div>
        <div class="layui-form-item" pane="">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="正常">
                <input type="radio" name="status" value="0" title="禁用" checked>
            </div>
        </div>
    </form>
</div>
</script>
<script>
    layui.data.sendParams = function(params) {
        const mode = params.mode, fromData = params.data || {};
        layui.use(['index', 'xmSelect', 'iconSelected', 'baseMethods'], function () {
            const {form, baseMethods, iconSelected} = layui;
            const parentSelect = layui.xmSelect.render({
                el: '#parent',
                name: 'parent_id',
                model: {label: {type: 'text'}},
                prop: {name: 'display_name', value: 'id'},
                radio: true,
                tips: '父级',
                clickClose: true,
                tree: {show: true, strict: false},
            });
            iconSelected.render("#icon", {value: (fromData.icon || "")});
            if (mode === 'add') {
                getPermission();
            }else if (mode === 'edit') {
                getPermission(fromData.parent_id, fromData.id);
                form.val('LAY-system-menus-form', fromData);
            }else if (mode === 'addChild') {
                getPermission(fromData.id);
            }
            function getPermission(parentId, selectId){
                baseMethods.request({
                    url: '/system/menus/list',
                    done: function (result){
                        if (result.code === 200){
                            let data = result.data;
                            if (selectId){
                                for (let index in data.rows){
                                    if (data.rows[index]['id'] === selectId || data.rows[index]['parent_id'] === selectId){
                                        data.rows.splice(index, 1);
                                    }
                                }
                            }
                            let list = AppGeneral.listToTree(data.rows, 'id', 'parent_id', 'children');
                            parentSelect.update({
                                data: list,
                                autoRow: true,
                                initValue: parentId ? [parentId] : [],
                            });
                            if (parentId > 0){
                                parentSelect.changeExpandedKeys([AppGeneral.parentId(list, parentId)])
                            }
                        }
                    }
                })
            }
        })
    }
</script>
